package utility;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

public class XMLUtil {

    /**
     * 将对象直接转换成String类型的 XML输出
     *
     * @param obj
     * @return
     */
    public static String ObjectToXml(Object obj) {
        // 创建输出流
        StringWriter sw = new StringWriter();
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }

    /**
     * 将对象根据路径转换成xml文件
     *
     * @param obj
     * @param path
     * @return
     */
    public static void ObjectToXml(Object obj, String path) {
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());

            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            // 创建输出流
            FileWriter fw = null;
            try {
                fw = new FileWriter(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            marshaller.marshal(obj, fw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    /**
     * xml转object
     *
     * @param clazz
     * @param xml
     * @param <T>
     * @return
     */
    public static <T> T XmlToObject(Class<T> clazz, String xml) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            StringReader reader = new StringReader(xml);
            T message = (T) unmarshaller.unmarshal(reader);
            return message;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }

//    public static void main(String[] args) {
//        Ingredients ingrd = new Ingredients();
//        ingrd.setMacCode("MAC001");
//        ingrd.setWipEntity("SN9999");
//        //
//        Item[] nn = new Item[]{
//                new Item("MAC001_1000", "12345678", "2020-10-23 15:45:23"),
//                new Item("MAC001_1001", "LUP", "2020-10-23 15:45:23"),
//                new Item("MAC001_1002", "301153902", "2020-10-23 15:45:23"),
//        };
//        ingrd.setItem(nn);
//        //
//        String xx = ObjectToXml(ingrd);
//        System.out.println(xx);
//    }
}
